package com.lcy.web.base

import org.jboss.resteasy.api.validation.ResteasyViolationException
import javax.ws.rs.Consumes
import javax.ws.rs.Produces
import javax.ws.rs.core.MediaType
import javax.ws.rs.core.Response
import javax.ws.rs.ext.ExceptionMapper
import javax.ws.rs.ext.Provider

/**
 * @author loucy
 */
@Provider
@Consumes(MediaType.APPLICATION_JSON+";charset=utf-8")
@Produces(MediaType.APPLICATION_JSON+";charset=utf-8")
class ValidatorExceptionMapper :ExceptionMapper<ResteasyViolationException>{
    override fun toResponse(exception: ResteasyViolationException): Response {
        val map = HashMap<String,String>()
        exception.violations.forEach {
            map.put(it.path,it.message)
        }
        return Response.ok(map,MediaType.APPLICATION_JSON_TYPE.withCharset("utf-8"))
                .build()
    }
}